package com.sky.service.common.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.common.DishMapper;
import com.sky.mapper.common.SetmealDishMapper;
import com.sky.mapper.common.SetmealMapper;
import com.sky.pojo.Setmeal;
import com.sky.pojo.SetmealDish;
import com.sky.result.PageResult;
import com.sky.service.common.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    private static final Logger log = LoggerFactory.getLogger(SetmealServiceImpl.class);
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;




    //分页查询
    @Override
    public PageResult findByPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> byPage = setmealMapper.findByPage(setmealPageQueryDTO);
        PageResult pageResult = new PageResult(byPage.getTotal(),byPage.getResult());
        return pageResult;
    }


    //添加套餐
    @Transactional
    @Override
    public void add(SetmealDTO setmealDTO) {
        log.info("接受的数据+套餐中的菜品:{}",setmealDTO);
        Setmeal setmeal = new Setmeal();

        BeanUtils.copyProperties(setmealDTO,setmeal);

        setmeal.setStatus(StatusConstant.DISABLE);

        setmealMapper.add(setmeal);
        //获取套餐的id
        Long id = setmeal.getId();
        //获取菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //给添加的菜品添加上套餐的id
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(id);
        });

        setmealDishMapper.insertBatch(setmealDishes);
    }



    //根据Id查询套餐
    @Override
    public SetmealVO findSetmealById(Long id) {
        SetmealVO setmealVO = setmealMapper.findSetmealById(id);

        return setmealVO;
    }



    //修改套餐
    @Override
    public void update(SetmealDTO setmealDTO) {

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //1、修改套餐表，执行update
        setmealMapper.update(setmeal);

        //套餐id
        Long setmealId = setmealDTO.getId();

        //2、删除套餐和菜品的关联关系，操作setmeal_dish表，执行delete
        setmealDishMapper.deleteBySetmealId(setmealId);
        //给菜品添加套餐id
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        //3、重新插入套餐和菜品的关联关系，操作setmeal_dish表，执行insert
        setmealDishMapper.insertBatch(setmealDishes);
    }



    //修改起售停售

    @Override
    public void startOrStop(Integer status, Long id) {
        // 使用建造者模式创建套餐对象，设置套餐ID和状态
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
//                .updateTime(LocalDateTime.now())
                .build();

                setmealMapper.startOrStop(setmeal);
    }



    //删除套餐
    @Transactional
    @Override
    public void delete(List<Long> ids) {
        //如果套餐正在起售中，则不能删除
        ids.forEach(id -> {
            SetmealVO setmealById = setmealMapper.findSetmealById(id);
            if (StatusConstant.ENABLE == setmealById.getStatus()){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });


        ids.forEach(setmealId -> {
            //根据id删除菜品
            dishMapper.deleteById(setmealId);
            //删除套餐
            setmealMapper.delete(ids);
        });
    }

    //根据分类id查询套餐
    @Override
    public List<SetmealVO> findSetmealBycategoryId(Integer categoryId) {
        List<SetmealVO> bycategoryId = setmealMapper.findSetmealBycategoryId(categoryId);
        return bycategoryId;
    }

    /**
     * 根据套餐id查询包含的菜品列表
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> findDishBySetmealId(Long id) {
        List<DishItemVO> dishBySetmealId = setmealMapper.findDishBySetmealId(id);

        return dishBySetmealId;
    }


}
